package com.kehutong.common.webchat;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.coraframework.util.Objects;
import org.coraframework.util.secret.Base64Coder;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 从微信lvbuffer
 * @author liuzhen
 *
 */
public class WxUserUtil extends DefaultHandler {

	private static final String DTD_URL = "http://apache.org/xml/features/nonvalidating/load-external-dtd";

	private String content;
	
	@Override
	public void startDocument() throws SAXException {
		
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
	
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		final String str = new String(ch, start, length).trim();
		if (Objects.nonEmpty(str)) {
			content = str;
		} else {
			content = "";
		}
	}
	
	public static List<String> xml2List(String lvbuffer) throws Exception {
		String content = Base64Coder.decodeString(lvbuffer);
		int index = content.indexOf("<atuserlist>");
		if (index == -1) {
			return Collections.emptyList();
		}
		
		content = content.substring(index);
		index = content.indexOf("</atuserlist>");
		if (index == -1) {
			return Collections.emptyList();
		}
		content = content.substring(0, index) + "</atuserlist>";
		return xml2List(new ByteArrayInputStream(content.getBytes()));
	}
	
	private static List<String> xml2List(InputStream input) throws Exception {
		WxUserUtil parse = new WxUserUtil();
		
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		// 禁止DTD解析，避免因找不到DTD文件导致解析失败。
		parser.getXMLReader().setFeature(DTD_URL, false);
		parser.parse(input, parse);
		
		String content = parse.content;
		if (Objects.isEmpty(content)) {
			return Collections.emptyList();
		}
		
		return Arrays.asList(content.split(","));
	}
	
	
	public static void main(String[] args) throws Exception {
		String content = Base64Coder.decodeString("ewAAAAAAAAB1PG1zZ3NvdXJjZT4KCTxhdHVzZXJsaXN0PjwhW0NEQVRBWzI1OTg0OTgyMDEzMjA5MTI5QG9wZW5pbSx3eGlkX3hiY3d1NGRkbnZtczIyLHl4amJvb2tzXV0+PC9hdHVzZXJsaXN0Pgo8L21zZ3NvdXJjZT4KAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH0=");
		System.out.println(content);
		int index = content.indexOf("<atuserlist>");
		
		content = content.substring(index);
		System.out.println(content);
		index = content.indexOf("</atuserlist>");
		content = content.substring(0, index);;
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx");
		System.out.println(content);
//		System.out.println(base.replace("\r", ""));
//		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
//		System.out.println(base.replace("\n", ""));
//		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
		String strss = "ewAAAAAAAAB1PG1zZ3NvdXJjZT4KCTxhdHVzZXJsaXN0PjwhW0NEQVRBWzI1OTg0OTgyMDEzMjA5MTI5QG9wZW5pbSx3eGlkX3hiY3d1NGRkbnZtczIyLHl4amJvb2tzXV0+PC9hdHVzZXJsaXN0Pgo8L21zZ3NvdXJjZT4KAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH0=";
		xml2List(strss).forEach(p->System.out.println(p));
	}
	
}
